package com.wuming.house.api;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.*;
import com.wuming.house.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 城市价格信息的接口
 */
@Controller
@RequestMapping("/api/price")
public class HousepriceApi {

    private static Logger logger = LoggerFactory.getLogger(HousepriceApi.class);

    @Autowired
    private HousepriceService housepriceService;

    /**
     * 根据城市查询房价信息
     *
     * @param city 城市
     * @return 房价信息
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public JsonResult getPriceByCity(@RequestParam String city) {
        try {
            List<Houseprice> houseprices = housepriceService.getHousepriceBetweenOneYear(city);
            HousepriceTrend ht = housepriceService.getTrendByHouseprices(houseprices);
            return new JsonResult(ht, "查询房价信息成功.", true);
        } catch (BusinessException be) {
            logger.error("查询房价信息失败,异常:{}", be.getMessage());
            return new JsonResult(null, "查询房价信息失败.", false);
        } catch (Exception e) {
            logger.error("查询房价信息失败,异常:{}", e.getMessage());
            return new JsonResult(null, "查询房价信息失败.", false);
        }
    }
}
